package binaryTree;

import java.util.Stack;

//TODO:再看
public class IsSubTree {

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
        public boolean isSubtree(TreeNode s, TreeNode t) {
            if (s == null) return false;
            else if (s.val == t.val && isSameTree(s, t)) return true;
            else return isSubtree(s.left, t) || isSubtree(s.right, t);
        }

        public boolean isSameTree(TreeNode n1, TreeNode n2) {
            if (n1 == null && n2 == null) return true;
            if (n1 == null || n2 == null) return false;
            return n1.val == n2.val && isSameTree(n1.left, n2.left) && isSameTree(n1.right, n2.right);
        }


    }



